package com.graduate.emos.wx.config.shiro;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


//Shiro配置类
@Configuration
public class ShiroConfig {

    /**
     * 配置SecurityManager对象用于封装Realm
     * @param realm Shiro认证和授权对象
     * @return SecurityManager
     */
    @Bean("securityManager")
    public SecurityManager createSecurityManager(OAuth2Realm realm){
        //创建SecurityManager的实现类对象
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //把OAuth2Realm封装到SecurityManager
        securityManager.setRealm(realm);
        //因为登录的凭证保存到客户端而服务端没有保存凭证因此setMememberMeManager()传null值
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    /**
     * 配置用于封装Filter对象以及设置Filter拦截路径的ShiroFilterFactoryBean
     * @param securityManager 封装Realm的对象
     * @param filter          过滤器
     * @return ShiroFilterFactoryBean
     */
    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean createShiroFilterFactoryBean(SecurityManager securityManager,OAuth2Filter filter){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        //设置SecurityManager
        factoryBean.setSecurityManager(securityManager);
        //封装OAuth2Filter过滤器
        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("oauth2",filter);
        factoryBean.setFilters(filterMap);

        //配置拦截路径和不拦截路径
        Map<String,String> routeMap = new LinkedHashMap<>();
        routeMap.put("/meeting/recieveNotify", "anon");
        routeMap.put("/message/sendMessage", "anon");
        routeMap.put("/webjars/**", "anon");//anon为不拦截
        routeMap.put("/druid/**","anon");
        routeMap.put("/app/**","anon");
        routeMap.put("/sys/login","anon");
        routeMap.put("/swagger/**","anon");
        routeMap.put("/swagger-ui/**","anon");
        routeMap.put("/v3/**","anon");
        routeMap.put("/swagger-resources/**","anon");
        routeMap.put("/captcha.jpg","anon");
        routeMap.put("/user/register","anon");
        routeMap.put("/user/login","anon");
        //routeMap.put("/test/**","anon");
        routeMap.put("/**","oauth2");//除了上面的不拦截 其余都拦截

        //将拦截路径配置到ShiroFilterFactoryBean
        factoryBean.setFilterChainDefinitionMap(routeMap);
        return factoryBean;
    }

    /**
     * 配置LifecycleBeanPostProcessor对象，管理Shiro对象生命周期
     * @return LifecycleBeanPostProcessor对象
     */
    @Bean("lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor createLifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    /**
     *创建AOP切面类用于在Web方法执行前验证权限
     * @param securityManager 封装Realm的对象
     * @return AuthorizationAttributeSourceAdvisor
     */
    @Bean("authorizationAttributeSourceAdvisor")
    public AuthorizationAttributeSourceAdvisor createAuthorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}
